package com.oyjd.beauty.fw.tool.face;

import java.util.ArrayList;
import java.util.List;

import com.oyjd.beauty.fw.util.FileOP;
/**
 * 解析接口
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class FaceParse {

	/**
	 * 分解接口成参数
	 * 
	 * @param path
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public static List<FunVO> parse(String path, String encoding) throws Exception {
		List<FunVO> list = new ArrayList<FunVO>();
		String c = FileOP.readFile(path, encoding);
		c = c.substring(c.indexOf("{") + 1, c.indexOf("}")).trim();
		String[] p = c.split(";");
		for (int i = 0; i < p.length; i++) {
			int pos = p[i].lastIndexOf("/");
			String note = p[i].substring(0, pos).trim();
			String face = p[i].substring(pos + 1).trim();
			FunVO fv = parseFun(face);
			parseNote(note, fv);
			list.add(fv);
		}
		return list;
	}

	/**
	 * 解析接口注释
	 * 
	 * @param note
	 * @param fv
	 */
	private static void parseNote(String note, FunVO fv) {
		note = note.replaceAll("\\*", "").replaceAll("/", "");
		int p1 = note.indexOf("@param");
		int p2 = note.indexOf("@return");
		if (-1 != p2) {
			String returnInfo = note.substring(p2 + "@return".length()).trim();
			fv.getBack().setDesc(returnInfo);
			note = note.substring(0, p2);
		}
		if (-1 != p1) {
			String[] pp = note.split("@param");
			fv.getFunName().setDesc(pp[0].trim());
			for (int i = 1; i < pp.length; i++) {
				FunParaVO para = fv.getParaList().get(i - 1);
				String desc = pp[i].replace(para.getName(), "").trim();
				para.setDesc(desc);
			}
		} else {
			fv.getFunName().setDesc(note.trim());
		}
	}

	/**
	 * 解析接口
	 * 
	 * @param face
	 * @return
	 */
	private static FunVO parseFun(String face) {
		FunVO fv = new FunVO();
		int p1 = face.indexOf(" ");
		int p2 = face.indexOf("(");
		int p3 = face.indexOf(")");

		String back = face.substring(0, p1);
		String funName = face.substring(p1, p2).trim();
		fv.getFunName().setName(funName);
		fv.getBack().setType(back);
		String ps = face.substring(p2 + 1, p3).trim();
		if (ps.length() > 0) {
			String[] ss = ps.split(",");
			for (int i = 0; i < ss.length; i++) {
				String[] se = ss[i].trim().split(" ");
				FunParaVO fpv = new FunParaVO();
				fpv.setType(se[0].trim());
				fpv.setName(se[1].trim());
				fv.getParaList().add(fpv);
			}
		}
		return fv;
	}
}
